﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using Rung.Net.Base;
using Rung.Net.Enums;
using Rung.Net.Events;

namespace Rung.Net.Session
{
    public class SessionStream : Simple.SimpleStream
    {
        #region Event

        /// <summary>
        /// Occurs when the stream is started.
        /// </summary>
        public event SessionEndEventHandler OnSessionEnd;

        #endregion
        public SessionStream(NetworkStream stream, EndPoint endpoint)
            : base(stream, endpoint)
        {
        }

        public override void Send(byte[] data)
        {
            if (data.Length > 512)
                throw (new Exception("不支持长度大于 512 的字节传送。"));
            else
                base.SendRaw(data);
        }

        protected override void ReceivedRaw(byte[] bytes)
        {
            RaiseOnReceived(bytes);
        }

        public void KillSession()
        {
            if (this.OnSessionEnd != null)
                this.OnSessionEnd(this, new SessionEndEventArgs(this.Guid));

            base.Stop(Rung.Net.Enums.NetStoppedReason.SessionEnd);
        }
    }
}
